<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class OxGeostamp 
{
	public $geonameid;
    public $name;
    public $latitude;
    public $longitude;
    public $feature_class;
    public $feature_code;
    public $country_code;
    public $country_code2;
    public $region_code1;
    public $region_code2;
    public $region_code3;
    public $region_code4;
    public $country_name;
		
    public static function from_id($geonameid)
    {
        $new_geostamp = new OxGeostamp();
        $new_geostamp->load($geonameid);
        return $new_geostamp;
    }
    
    public static function fromids($geonameids)
    {
        $CI =& get_instance();
        $id_list = $query = $CI->db->select('geonameid')
                              ->from('geo_location')
                              ->where('geonameid in ('.$geonameids.')')
                              ->get()->result();
        $res = array();
        foreach($id_list as $id)
        {
            $res[] = OxGeostamp::from_id($id->geonameid);
        }
        return $res;
    }
    
    function __construct()
    {
		$this->clear();
	}
    
    function clear()
    {
        $this->geonameid = null;
        $this->name = null;
        $this->latitude = null;
        $this->longitude = null;
        $this->feature_code = null;
        $this->feature_class = null;
        $this->country_code = null;
        $this->country_code2 = null;
        $this->region_code1 = null;
        $this->region_code2 = null;
        $this->region_code3 = null;
        $this->region_code4 = null;
        $this->country_name = null;
        
        return $this;
    }
    
    function load($geonameid)
    {
        $CI =& get_instance();
        
		$query = $CI->db->select('geo_location.*')
                              ->select('geo_countries.name as country_name')
                              ->from('geo_location')
                              ->join('geo_countries', 'geo_location.country_code = geo_countries.idcountries', 'left')
                              ->where('geo_location.geonameid',$geonameid)
                              ->get()->result();
		if(!$query)
        {
            $this->clear();
        }
        else
        {
            $stampdata = $query[0];
            $this->geonameid = $stampdata->geonameid;
            $this->name = $stampdata->name;
            $this->latitude = $stampdata->latitude;
            $this->longitude = $stampdata->longitude;
            $this->feature_code = $stampdata->feature_code;
            $this->feature_class = $stampdata->feature_class;
            $this->country_code = $stampdata->country_code;
            $this->country_code2 = $stampdata->country_code2;
            $this->region_code1 = $stampdata->region_code1;
            $this->region_code2 = $stampdata->region_code2;
            $this->region_code3 = $stampdata->region_code3;
            $this->region_code4 = $stampdata->region_code4;
            if($this->country_code)
            {
                $this->country_name = $stampdata->country_name;
            }
        }
        return $this;
    }

    //This function should be a lot expanded, to include regions specific for each country.
    //Alternatively we can store the actual showing with each stamp, so it does
    //not need to be calculated again.
    function show()
    {
        //For countries, show only the country name
        if($this->feature_class == 'A' && $this->feature_code == 'PCLI')
        {
            return $this->country_name;
        }
        
        //For other places, show name, and if a country is set, country name
        $s = $this->name;
        if($this->country_code)
        {
            $s .= ', ' . $this->country_code;
        }
        return $s;
    }
    
    
} 